<?php

declare(strict_types=1);

namespace App\Service;

use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;
use App\Model\Dao\TestDao;
use App\Exception\XZXValidationException;
use App\Utils\DateUtils;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use App\Task\TestTask;
use App\Task\EmailTask;
use App\Constants\XZXResponseCode;

class TestService
{
    /**
     * @Inject()
     * @var TestDao
     */
    private $TestDao;

    public function sendMail($param){
        //Task 的 示例 开始
        $container = ApplicationContext::getContainer();
        $task = $container->get(EmailTask::class);
        $result = $task->handle($param);
        CustomUtils::xzxDebug($result);
        //Task 的 示例结束
    }

    public function get($param)
    {
        $result = $this->TestDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        if(isset($result['createTime'])){
            $result['createTime'] = DateUtils::getDateTime($result['createTime']);
        }
        $container = ApplicationContext::getContainer();

        //Task 的 示例 开始
        $task = $container->get(TestTask::class);
        $result = $task->handle(Coroutine::id());
        CustomUtils::xzxDebug($result);
        //Task 的 示例结束

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function del($param)
    {
        $result = $this->TestDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        $result = $this->TestDao->del($param['id']);
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function add($param)
    {
        // TODO: Implement add() method.
        $data            =  [
            'name'       => $param['name'],
            'desciption' => $param['desciption'],

        ];
        CustomUtils::xzxDebug($data);
        $this->TestDao->add($data);
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function edit($param)
    {
        $info = $this->TestDao->get($param['id']);
        if (!$info) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $id = $param['id'];
        $data = array();
        // isset($param['name']) && $data['name'] = $param['name'];
        // isset($param['desciption']) && $data['desciption'] = $param['desciption'];

        foreach($param as $k => $v){
            if($k == "id"){
                continue;
            }
            if(isset($v)){
                $data[$k] = $v;
            }
        }
        CustomUtils::xzxDebug($data);


        $this->TestDao->update($id, $data);
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];

        $total = $this->TestDao->getCount($param);
        $list = $this->TestDao->getList($param, $page, $limit);

        CustomUtils::xzxDebug($list);
        foreach ($list as $k => $v) {
            CustomUtils::xzxDebug($v);
            if(isset($v['createTime'])){
                $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            }
            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );



        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }
}
